package com.ruoyi.project.socket.packet;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class L1Packet {

    private L1Header l1Header;

    private L2Packet l1Payload;

    public byte[] l1Packet2Bytes(){
        ByteBuf buf = Unpooled.buffer(l1Header.getLen(),l1Payload.getLen());
        buf.writeBytes(l1Header.l1Header2Bytes());
        byte[]l1PayloadBytes = l1Payload.l2Packet2Bytes();
        if(null==l1PayloadBytes)
            return null;
        buf.writeBytes(l1PayloadBytes);
        return buf.array();
    }

    public L1Packet(L1Header l1Header, L2Packet l1Payload) {
        if(null==l1Header||l1Payload==null){
            throw new RuntimeException("l1Header或l1Payload为空");
        }
        this.l1Header = l1Header;
        this.l1Payload = l1Payload;
    }

    public L1Header getL1Header() {
        return l1Header;
    }

    public void setL1Header(L1Header l1Header) {
        this.l1Header = l1Header;
    }

    public L2Packet getL1Payload() {
        return l1Payload;
    }

    public void setL1Payload(L2Packet l1Payload) {
        this.l1Payload = l1Payload;
    }
}
